package org.footito.android.ui.adapters;

import java.util.ArrayList;

import org.footito.android.R;
import org.footito.android.data.Comment;
import org.footito.android.data.Status;
import org.footito.android.data.database.FootitoDatabaseAdapter;
import org.footito.android.tools.Log;
import org.footito.android.ui.views.InformationView;
import org.footito.android.ui.views.TimelineRow;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class TimelineAdapter extends BaseExpandableListAdapter {

	private FootitoDatabaseAdapter dbAdapter;
	private ArrayList<Status> statusList = new ArrayList<Status>();
	private Context context;
	private ListView list;
	private int addedCount = 0;
	private Runnable action = new Runnable() {
		@Override
		public void run() {
			int position = list.getFirstVisiblePosition();
			TimelineAdapter.this.notifyDataSetChanged();
			list.setSelection(position + addedCount);
			addedCount = 0;
		}
	};

	public TimelineAdapter(Context context, ListView list) {
		dbAdapter = FootitoDatabaseAdapter.getInstance(context);
		statusList = dbAdapter.getAllStatus();
		this.context = context;
		this.list = list;
	}

	public void reloadStatus(Activity activity) {
		int firstCount = statusList.size();
		statusList = dbAdapter.getAllStatus();
		addedCount = statusList.size() - firstCount;
		activity.runOnUiThread(action);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return statusList.get(groupPosition).getComments().get(childPosition - 1);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		try {
			Status status = statusList.get(groupPosition);
			Comment comment = status.getComments().get(childPosition - 1);
			return comment.getId();
		} catch (Exception e) {
			Log.e(e);
			return 0;
		}
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		if (childPosition == 0) {
			convertView = new InformationView(context);
			((InformationView) convertView).setStatus(statusList.get(groupPosition));
		} else {
			convertView = LayoutInflater.from(context).inflate(R.layout.row_comment, null);
			TextView tv_comment = (TextView) convertView.findViewById(R.id.tv_comment);
			ArrayList<Comment> commentsList = statusList.get(groupPosition).getComments();
			if (childPosition - 1 < 3 || commentsList.size() == 4) {
				Comment comment = commentsList.get(childPosition - 1);
				Spanned text = Html.fromHtml("<b>" + comment.getTeam().getName() + "</b> : " + comment.getContent());
				tv_comment.setText(text);
			} else {
				Spanned text = Html.fromHtml("<b>...</b>");
				tv_comment.setText(text);
				tv_comment.setGravity(Gravity.CENTER);
			}
		}
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (statusList.get(groupPosition).getComments().size() <= 4) {
			return statusList.get(groupPosition).getComments().size() + 1;
		} else {
			return 4 + 1;
		}
	}

	@Override
	public Object getGroup(int groupPosition) {
		return statusList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return statusList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return statusList.get(groupPosition).getId();
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		if (convertView == null)
			convertView = new TimelineRow(context);
		((TimelineRow) convertView).setStatus(statusList.get(groupPosition));
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}
